package com.chuanyang.driverh.mainorder.fragment;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.amap.MapUtils;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainorder.SureUnLoadOverActivity;
import com.chuanyang.driverh.mainorder.WayBillDetailActivity;
import com.chuanyang.driverh.mainother.BeforeNaviActivity;
import com.chuanyang.driverh.responseresult.CarCurrentStatusResult;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.responseresult.DriverWayBillData;
import com.chuanyang.driverh.responseresult.DriverWayBillResult;
import com.chuanyang.driverh.responseresult.OrderAddressResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.responseresult.ValidateCodeResult;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.TimeUtils;
import com.chuanyang.driverh.utils.eventbus.RefleshDriverInfoEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 我的运单
 */
public class WaybillFragment extends BaseListFragment {
    private AlertDialog alertDialog;
    private MyOrderAdapter orderAdaoter;
    private DriverInfoData driverinfo;
    private EditText et_sureunload_code;
    private TextView tv_cancle_unload;
    private TextView tv_sureunload_getcode;
    private TextView tv_sure_unload;
    private boolean validateOk;
//    private boolean isFirst=true;
    private boolean hasGetData;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1://确认收货
                    updateWaybillUnload((String) msg.obj);
                    break;
            }
        }
    };

    @Override
    public BaseListAdapter createAdapter() {
        return orderAdaoter = new MyOrderAdapter(getActivity());
    }

    @Override
    protected void initData() {
        refreshDriverInfo();
    }

    public void refreshDriverInfo() {
        hasGetData=true;
        driverinfo = BaseApplication.getInstance().getDriverInfo();
        if (driverinfo==null)return;
        if (driverinfo.getDriver() != null && driverinfo.getDriver().getCarId() != 0) {
            GetCarCurrentStatusByCarID(driverinfo.getDriver().getCarId() + "");
        } else {
            GetCarCurrentStatusByCarID("");
        }
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }
    @Override
    public void onRefresh() {
        if (!hasGetData)return;
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected void onForceRefresh() {
        super.onForceRefresh();
        onRefresh();

    }

    @Override
    public void onGetMore() {
        getData();
    }




  /*  public void onEventMainThread(RefleshOrderData event) {
        if (event.getIndex() == 2) {
            onRefresh();
        }
    }

*/
      public void onEventMainThread(RefleshDriverInfoEvent event) {
          refreshDriverInfo();
      }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        DriverWayBillData.DataBean waybill = (DriverWayBillData.DataBean) orderAdaoter.getItem(position);
        startActivity(WayBillDetailActivity.class, waybill.getWaybillId() + "");

    }


    /**
     * 是否显示确认卸货按钮（此处通过判断距离目的地距离，25公里之内则显示，否则不显示）
     *
     * @param latAndLng
     * @return
     */
    public boolean showUnload(String latAndLng) {
        String lngCurrentStr = SharedPrefUtil.getLongitude();
        String latCurrentStr = SharedPrefUtil.getLatitude();
        LogUtils.e("latTargetlngCurrentStr ", lngCurrentStr + "-------" + latCurrentStr);
        if (TextUtils.isEmpty(lngCurrentStr) || TextUtils.isEmpty(latCurrentStr)) {
            return false;
        }
        String[] latlang = latAndLng.split(",");
        double lngCurrent = Double.parseDouble(lngCurrentStr);
        double latCurrent = Double.parseDouble(latCurrentStr);
        double lngTarget = Double.parseDouble(latlang[1]);
        double latTarget = Double.parseDouble(latlang[0]);
        LatLng latlngCurrent = new LatLng(latCurrent, lngCurrent);
        LatLng latlngTarget = new LatLng(latTarget, lngTarget);
        float distance = AMapUtils.calculateLineDistance(latlngCurrent, latlngTarget);
        if (distance < 2500 * 1000) {
            return true;
        }
        return false;
    }


    /**
     * 获取订单或者运单的列表
     */
    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", mCurrentPage + "");
        params.put("length", "10");

        MyHttpUtil.selectDriverWayBillList(getActivity(),params, new MyCallback<DriverWayBillResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                springView.onFinishFreshAndLoad();
                onShowFailed();
            }
            @Override
            public void onSuccess(DriverWayBillResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    setDataResult(result.getData().getData());
//                    isFirst=false;
                }else{
                    onShowNull();
                    springView.onFinishFreshAndLoad();
//                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }


    private void selectOrderAddress(String id) {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("orderId", id);
        MyHttpUtil.selectOrderAddress(getActivity(),params, new MyCallback<OrderAddressResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(OrderAddressResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    Intent intent = new Intent(getActivity(), BeforeNaviActivity.class);
                    intent.putExtra("OrderAddressResult", result);
                    startActivity(intent);
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    /**
     * 通过车辆id获取车辆位置
     * CarID
     *
     * @param
     */
    private void GetCarCurrentStatusByCarID(String carId) {
        Map<String,String> params=new HashMap<String, String>();
        params.put("CarID", carId);
        MyHttpUtil.GetCarCurrentStatusByCarID(getActivity(),params, new MyCallback<CarCurrentStatusResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                onRefresh();
            }
            @Override
            public void onSuccess(CarCurrentStatusResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    if (result.getData() != null && result.getData().size() > 0) {
                        LatLng targetLatLng = MapUtils.getGdLatLng(getActivity(), result.getData().get(0).getLast_lat(), result.getData().get(0).getLast_lon());
                        SharedPrefUtil.setLongitude(targetLatLng.longitude + "");
                        SharedPrefUtil.setLatitude(targetLatLng.latitude + "");
                    }
                }
                onRefresh();
            }
        });
    }

    /**
     * 修改运单状态
     */
    private void startTranslate(final DriverWayBillData.DataBean waybill, final int position) {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("waybillId", waybill.getWaybillId() + "");
        params.put("waybillStatus", "4");
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.startTranslate(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    waybill.setWaybillStatus(4);
                    mAdapter.replaceOnedata(position, waybill);
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });


    }

    /**
     * 提示司机输入收货人验证码
     */
    public void sureAcceptGood(final String phone, final String id) {

        if (null == alertDialog) {
            alertDialog = new AlertDialog.Builder(getActivity()).create();
            alertDialog.show();
            alertDialog.setTitle(null);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    alertDialog = null;
                }
            });
            Window window = alertDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            window.setBackgroundDrawable(new ColorDrawable(0));
            window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            window.setContentView(R.layout.dialog_sureunload);
            View view = window.getDecorView();
            et_sureunload_code = (EditText) view.findViewById(R.id.et_sureunload_code);
            tv_cancle_unload = (TextView) view.findViewById(R.id.tv_cancle_unload);
            tv_sureunload_getcode = (TextView) view.findViewById(R.id.tv_sureunload_getcode);
            tv_sure_unload = (TextView) view.findViewById(R.id.tv_sure_unload);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    validateOk = false;

                }
            });
            tv_cancle_unload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });
            tv_sureunload_getcode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!validateOk) {//获取成功验证码 且对话框没消失时无法多次点击
                        getValidateCode(phone, id);
                    } else {
                        PrintUtil.toast(getActivity(),"验证码已发送给货主！");
                    }
                }
            });
            tv_sure_unload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(et_sureunload_code.getText().toString().trim())) {
                        PrintUtil.toast(getActivity(),"请填写获取到验证码");
                        return;
                    }
                    if (!validateOk){
                        PrintUtil.toast(activityContext,"请先获取验证码");
                        return;
                    }
                    Message message = Message.obtain();
                    message.obj = id;
                    message.what = 1;
                    mHandler.sendMessage(message);
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });
        }
        //下面两行代码加入后即可弹出输入法
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

    private void getValidateCode(String phone, String id) {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("phone", phone);
        params.put("validateMsg", id);
        MyHttpUtil.getSureValidateCode(getActivity(),params, new MyCallback<ValidateCodeResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(ValidateCodeResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    validateOk = true;
                    tv_sureunload_getcode.setBackgroundDrawable(getResources().getDrawable(R.drawable.tv_shape_gray_round_corner_solid));
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });


    }

    /**
     * 收货完成 （传入订单id或者运单id）
     */
    private void updateWaybillUnload(String id) {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("waybillId", id);

        params.put("waybillStatus", "7");
        params.put("validateMsg", id + "");
        params.put("validateCode", et_sureunload_code.getText().toString().trim());
        MyHttpUtil.updateWaybillStatus(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(getActivity(),"您已替货主确认收货");
                    mCurrentPage = 1;
                    getData();
                }else{
//                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });


    }







    private class MyOrderAdapter extends BaseListAdapter {

        private ViewHolder viewHolder;
        private DriverWayBillData.DataBean bean2;

        public MyOrderAdapter(Context context) {
            super(context);
        }

        @Override
        public View getZkView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getInflater().inflate(R.layout.common_item_mywaybill, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.ll_driverbill_account = (LinearLayout) convertView.findViewById(R.id.ll_driverbill_account);
                viewHolder.iv_myorder_image = (ImageView) convertView.findViewById(R.id.iv_myorder_image);
                viewHolder.iv_myorder_fleedpersonal = (ImageView) convertView.findViewById(R.id.iv_myorder_fleedpersonal);//车队订单是个人订单
                viewHolder.rb_stars = (RatingBar) convertView.findViewById(R.id.rb_stars);
                viewHolder.tv_myorder_ordertype = (TextView) convertView.findViewById(R.id.tv_myorder_ordertype);
                viewHolder.tv_myorder_name = (TextView) convertView.findViewById(R.id.tv_myorder_name);
                viewHolder.tv_myorder_type = (TextView) convertView.findViewById(R.id.tv_myorder_type);
                viewHolder.tv_myorder_startadd = (TextView) convertView.findViewById(R.id.tv_myorder_startadd);
                viewHolder.tv_myorder_endadd = (TextView) convertView.findViewById(R.id.tv_myorder_endadd);
                viewHolder.tv_myorder_starttim = (TextView) convertView.findViewById(R.id.tv_myorder_starttim);
                viewHolder.tv_myorder_endtim = (TextView) convertView.findViewById(R.id.tv_myorder_endtim);
                viewHolder.tv_myorder_carshipstarttim = (TextView) convertView.findViewById(R.id.tv_myorder_carshipstarttim);
                viewHolder.tv_myorder_carshipendtim = (TextView) convertView.findViewById(R.id.tv_myorder_carshipendtim);
                viewHolder.tv_myorder_num = (TextView) convertView.findViewById(R.id.tv_myorder_num);
                viewHolder.tv_myorder_createtime = (TextView) convertView.findViewById(R.id.tv_myorder_createtime);
                viewHolder.tv_myorder_load = (TextView) convertView.findViewById(R.id.tv_myorder_load);
                viewHolder.tv_myorder_sum = (TextView) convertView.findViewById(R.id.tv_myorder_sum);
                viewHolder.tv_myorder_goodname = (TextView) convertView.findViewById(R.id.tv_myorder_goodname);
                viewHolder.tv_myorder_amount = (TextView) convertView.findViewById(R.id.tv_myorder_amount);
                // viewHolder.tv_myorder_time = (TextView) convertView.findViewById(R.id.tv_myorder_time);
                viewHolder.tv_myorder_status = (TextView) convertView.findViewById(R.id.tv_myorder_status);//订单状态
                viewHolder.bt_myorder_act1 = (TextView) convertView.findViewById(R.id.bt_myorder_act1);
                viewHolder.bt_myorder_act2 = (TextView) convertView.findViewById(R.id.bt_myorder_act2);
                viewHolder.tv_myorder_orginamount = (TextView) convertView.findViewById(R.id.tv_myorder_orginamount);//下单金额
                viewHolder.tv_myorder_tradetype = (TextView) convertView.findViewById(R.id.tv_myorder_tradetype);//订单外贸类型（1人民币，2美元）
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.bt_myorder_act2.setVisibility(View.GONE);//初始化时第二个按钮不可见    iv_myorder_fleedpersonal
            bean2 = (DriverWayBillData.DataBean) getItem(position);
            viewHolder.ll_driverbill_account.setVisibility(View.VISIBLE);
            viewHolder.tv_myorder_type.setText("运单号");
            BitmapUtil.showImgForNetPath(bean2.getUserImage(), viewHolder.iv_myorder_image);
//            YncApplication.getInstance().loadCircleImage(getActivity(), bean2.getUserImage(), R.drawable.shengfeng, R.drawable.shengfeng, viewHolder.iv_myorder_image);
            viewHolder.tv_myorder_name.setText(bean2.getUsername());
            viewHolder.rb_stars.setRating(bean2.getStar());
            viewHolder.tv_myorder_startadd.setText(StringUtil.getSimpleMunicipality(bean2.getStartAddr()));
            viewHolder.tv_myorder_endadd.setText(StringUtil.getSimpleMunicipality(bean2.getTargetAddr()));

            viewHolder.tv_myorder_num.setText(bean2.getWaybillNo());
          if (bean2.getGoodsSum()!=null ) {
              viewHolder.tv_myorder_sum.setText(bean2.getGoodsSum().getGoodsNum() + "");
              viewHolder.tv_myorder_load.setText(bean2.getGoodsSum().getGoodsWeight() + "t");
          }
            viewHolder.tv_myorder_createtime.setText(TimeUtils.getStandardTime(bean2.getCreatetime()));
            setGoodName(viewHolder.tv_myorder_goodname, bean2.getBidGoodsName());
            viewHolder.iv_myorder_fleedpersonal.setImageResource(R.mipmap.individual);

            viewHolder.tv_myorder_orginamount.setText("");
            if(bean2.getCurrency()==2)//订单外贸类型（1人民币，2美元）
            {
                viewHolder.tv_myorder_tradetype.setVisibility(View.VISIBLE);
            }
            else {
                viewHolder.tv_myorder_tradetype.setVisibility(View.GONE);

            }
            if (bean2.getTransPortWay() == 1)//车
            {
                viewHolder.tv_myorder_starttim.setText(TimeUtils.getStandardTime(bean2.getOrder_startime()));
                viewHolder.tv_myorder_endtim.setText(TimeUtils.getStandardTime(bean2.getOrder_endtime()));
                viewHolder.tv_myorder_carshipstarttim.setText("接货");
                viewHolder.tv_myorder_carshipendtim.setText("卸货");
            } else if (bean2.getTransPortWay() == 2)//船
            {
                viewHolder.tv_myorder_starttim.setText(bean2.getLoadDate());
                viewHolder.tv_myorder_endtim.setText(bean2.getStevedoreDay());
                viewHolder.tv_myorder_carshipstarttim.setText("受载日期");
                viewHolder.tv_myorder_carshipendtim.setText("装卸期限");
            }
            //根据订单类型确定显示
            if (bean2.getOrder_type().equals(1))//竞价
            {
                viewHolder.tv_myorder_ordertype.setText("竞价");
                viewHolder.tv_myorder_tradetype.setBackgroundColor(getResources().getColor(R.color.compete));
                viewHolder.tv_myorder_ordertype.setBackgroundResource(R.drawable.tv_shape_order_compete);
            } else if (bean2.getOrder_type().equals(2))//匹配
            {
                viewHolder.tv_myorder_ordertype.setText("匹配");
                viewHolder.tv_myorder_tradetype.setBackgroundColor(getResources().getColor(R.color.match));

                viewHolder.tv_myorder_ordertype.setBackgroundResource(R.drawable.tv_shape_order_macher);
            } else {//专线
                viewHolder.tv_myorder_ordertype.setText("专线");
                viewHolder.tv_myorder_tradetype.setBackgroundColor(getResources().getColor(R.color.specialline));
                viewHolder.tv_myorder_ordertype.setBackgroundResource(R.drawable.tv_shape_order_special);
            }
            //根据金额是否有金额确认显示与否
            if (bean2.getPrice() != null) {
                if (bean2.getGoodsSum()!=null ) {
                    viewHolder.tv_myorder_amount.setText(bean2.getPrice() + bean2.getUnit()+"/t");
                    double  orginAmount=bean2.getGoodsSum().getGoodsWeight() * bean2.getPrice();
                    viewHolder.tv_myorder_orginamount.setText("总金额：" + String.format("%.2f", orginAmount )+bean2.getUnit() );

                }

            } else {
                viewHolder.ll_driverbill_account.setVisibility(View.GONE);
            }
            if (bean2.getWaybillStatus() < 0) {//运单全是个人的 且不存在状态1
                viewHolder.bt_myorder_act1.setVisibility(View.GONE);
                viewHolder.tv_myorder_status.setText("正在处理");
            }
            if (bean2.getWaybillStatus() == 2) {
                viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                viewHolder.bt_myorder_act1.setText("开始运输");
                viewHolder.tv_myorder_status.setText("待装货");
            } else if (bean2.getWaybillStatus() == 3) {
                viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                viewHolder.tv_myorder_status.setText("装货中");
                viewHolder.bt_myorder_act1.setText("装货向导");
            } else if (bean2.getWaybillStatus() == 4) {
                if (bean2.getTransPortWay() == 1)//车
                {
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act1.setText("导航");
                } else if (bean2.getTransPortWay() == 2) {
                    viewHolder.bt_myorder_act1.setVisibility(View.GONE);
                }
                viewHolder.bt_myorder_act2.setVisibility(View.VISIBLE);
                viewHolder.bt_myorder_act2.setText("运输完成");
                viewHolder.tv_myorder_status.setText("运输中");
            } else if (bean2.getWaybillStatus() == 5) {
                if (showUnload(bean2.getLatAndLng())) {
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act1.setText("卸货向导");
                    viewHolder.bt_myorder_act2.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act2.setText("运输完成");
                } else {
                    viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                    viewHolder.bt_myorder_act1.setText("卸货向导");
                }
                viewHolder.tv_myorder_status.setText("卸货中");
            } else if (bean2.getWaybillStatus() == 6) {
                viewHolder.bt_myorder_act1.setVisibility(View.VISIBLE);
                viewHolder.bt_myorder_act1.setText("验证卸货");
                viewHolder.tv_myorder_status.setText("卸货完成，等待验证卸货");
            }
            viewHolder.bt_myorder_act1.setOnClickListener(new View.OnClickListener() {//由于车队订单只有状态1时显示，所以点击其按钮只需要判断其订单状态，无需判断是否是车队了
                @Override
                public void onClick(View v) {
                    bean2 = (DriverWayBillData.DataBean) getItem(position);
                    if (bean2.getWaybillStatus() == 2) {
                        startTranslate(bean2, position);

                    } else if (bean2.getWaybillStatus() == 3) {
                        PrintUtil.toast(getActivity(),"装货向导");
                    } else if (bean2.getWaybillStatus() == 4) {
                        // showMsg("导航");
                        //requsetWayBillData(bean2);
                        selectOrderAddress(bean2.getOrderId() + "");
                    } else if (bean2.getWaybillStatus() == 5) {
                        PrintUtil.toast(getActivity(),"卸货向导");
                    } else if (bean2.getWaybillStatus() == 6) {
                        sureAcceptGood(bean2.getContactPhone(), bean2.getWaybillId() + "");

                    }
                }
            });
            viewHolder.bt_myorder_act2.setOnClickListener(new View.OnClickListener() {
                                                              @Override
                                                              public void onClick(View v) {
                                              bean2 = (DriverWayBillData.DataBean) getItem(position);
                                              Intent intent = new Intent(getActivity(), SureUnLoadOverActivity.class);
                                              bean2 = (DriverWayBillData.DataBean) getItem(position);
                                              //  intent.putExtra("phone", bean2.getPhone());
                                              intent.putExtra("waybillId", bean2.getWaybillId());
                                              intent.putExtra("fromlist", true);
                                              startActivity(intent);
                                          }


                                      }
            );
            return convertView;
        }
        private void setGoodName(TextView view, List<String> goodname) {
            String name = StringUtil.getString(goodname);
            view.setText(name);
        }
        class ViewHolder {
            LinearLayout ll_driverbill_account;
            ImageView iv_myorder_image;
            ImageView iv_myorder_fleedpersonal;//是个人的订单还是车队的订单
            TextView tv_myorder_name;
            TextView tv_myorder_type;//运单还是订单
            TextView tv_myorder_startadd;
            // TextView  tv_myorder_passadd;
            TextView tv_myorder_status;//订单状态
            TextView tv_myorder_endadd;
            TextView tv_myorder_starttim;
            TextView tv_myorder_endtim;
            TextView tv_myorder_carshipstarttim;//接货  受载日期
            TextView tv_myorder_carshipendtim;//卸货  装载期限
            TextView tv_myorder_num;
            TextView tv_myorder_createtime;
            TextView tv_myorder_load;
            TextView tv_myorder_sum;//数量
            RatingBar rb_stars;
            TextView tv_myorder_ordertype;//订单类型
            TextView tv_myorder_goodname;
            TextView tv_myorder_amount;
            TextView tv_myorder_orginamount;//下单金额
            TextView tv_myorder_time;
            TextView bt_myorder_act1;
            TextView bt_myorder_act2;
            TextView tv_myorder_tradetype;
        }
    }
}


